package com.googlecode.openauth.web.action;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;

import com.googlecode.openauth.web.config.CommonProperty;
import com.googlecode.openauth.web.dao.IActionDao;
import com.googlecode.openauth.web.dao.IAreaDao;
import com.googlecode.openauth.web.dao.IMenuDao;
import com.googlecode.openauth.web.dao.IRelationDao;
import com.googlecode.openauth.web.dao.IResourceDao;
import com.googlecode.openauth.web.dao.IRoleDao;
import com.googlecode.openauth.web.entity.ActionInfo;
import com.googlecode.openauth.web.entity.ActionPageBean;
import com.googlecode.openauth.web.entity.AreaInfo;
import com.googlecode.openauth.web.entity.MenuInfo;
import com.googlecode.openauth.web.entity.RMenu;
import com.googlecode.openauth.web.entity.RRole;
import com.googlecode.openauth.web.entity.RRoleArea;
import com.googlecode.openauth.web.entity.RRoleResource;
import com.googlecode.openauth.web.entity.ResourceInfo;
import com.googlecode.openauth.web.entity.RoleInfo;
import com.googlecode.openauth.web.entity.UserInfo;
import com.googlecode.openauth.web.util.Util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author Administrator
 * 
 */
public class RoleAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9181754399704066108L;

	private IRoleDao roleDao;
	private IActionDao actionDao;
	private IResourceDao resourceDao;
	private IAreaDao areaDao;
	private IRelationDao relationDao;
	private IMenuDao menuDao;
	private int roleId;
	private int[] resIds;
	private int[] menuIds;
	private int[] roleIds;
	private String uuid;
	private String roleName;
	private String roleDesc;
	private int createUserId;
	private Timestamp createDate;
	private Timestamp updateDate;
	private List<RoleInfo> list;
	private String oper = "list";
	private int actionIds[];
	private List<ResourceInfo> reslist;
	private List<ActionInfo> actlist;
	private List<RRoleResource> roleResList;
	private List<MenuInfo> menulist;
	private List<RMenu> rmenuList;
	private List<AreaInfo> areaList;

	private ActionPageBean<RoleInfo> actionPageBean;
	private int currentPageNo = 1;
	private int prevPageNo = 1;
	private int nextPageNo = 1;
	private int totalCount = 0;
	private int areaId;
	private int pageNum = CommonProperty.PAGENUM;

	private List<com.googlecode.openauth.web.entity.RoleAction> roleActionList;

	public String detail() throws Exception{
		ResourceInfo res=new ResourceInfo();
		res.setRoleId(roleId);
		reslist=this.resourceDao.getResources(res);
		
		ActionInfo act=new ActionInfo();
		act.setRoleId(roleId);
		actlist=this.actionDao.getActions(act);
		MenuInfo menu=new MenuInfo();
		menu.setRoleId(roleId);
		
		AreaInfo area=new AreaInfo();
		area.setRoleId(roleId);
		areaList=this.areaDao.searchAreas(area,new RowBounds());
		menulist=menuDao.getMenus(menu);
		RoleInfo role = roleDao.getRoleById(roleId);
		setForm(role);
		return "success";
	}
	@Override
	public String execute() throws Exception {
		RoleInfo role = this.getForm();
		role.setRoleId(getCurRole().getRoleId());
		list = roleDao.getRoles(role);
		actionPageBean = new ActionPageBean<RoleInfo>();
		actionPageBean.setData(list);
		return SUCCESS;
	}

	
	private UserInfo getCurUser() {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		Object obj = session.get("user");
		UserInfo result = new UserInfo();
		result.setUserId(-1);
		if (obj == null) {
			return result;
		}

		if (!(obj instanceof UserInfo)) {
			return result;
		}

		result = (UserInfo) obj;
		return result;
	}

	public RoleInfo getCurRole() {
		RoleInfo result = new RoleInfo();
		result.setRoleId(-1);

		UserInfo userInfo = getCurUser();
		List<RoleInfo> roles = userInfo.getRoleInfos();

		if (roles == null || roles.isEmpty()) {
			return result;
		}

		result = roles.get(0);
		return result;
	}

	public String getFirstPage() throws Exception {
		RoleInfo curRole = getCurRole();
		int totalCount = roleDao.getAllRolesCount(curRole);
		actionPageBean = new ActionPageBean<RoleInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<RoleInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		pageBean.setPrevPageNo(1);
		pageBean.setCurrentPageNo(1);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		if (maxPageNo <= 1) {
			pageBean.setNextPageNo(1);
		} else {
			pageBean.setNextPageNo(2);
		}

		int offset = 0;
		int limit = pageBean.getPageNum();
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<RoleInfo> data = roleDao.getAllRoles(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getPrevPage() throws Exception {
		RoleInfo curRole = getCurRole();
		int totalCount = roleDao.getAllRolesCount(curRole);
		actionPageBean = new ActionPageBean<RoleInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<RoleInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		setPageBean(pageBean);
		List<RoleInfo> data = roleDao.getAllRoles(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getNextPage() throws Exception {
		RoleInfo curRole = getCurRole();
		int totalCount = roleDao.getAllRolesCount(curRole);
		actionPageBean = new ActionPageBean<RoleInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<RoleInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		setPageBean(pageBean);
		List<RoleInfo> data = roleDao.getAllRoles(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getLastPage() throws Exception {
		RoleInfo curRole = getCurRole();
		int totalCount = roleDao.getAllRolesCount(curRole);
		actionPageBean = new ActionPageBean<RoleInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<RoleInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		pageBean.setCurrentPageNo(maxPageNo);
		pageBean.setNextPageNo(maxPageNo);

		if (maxPageNo <= 1) {
			pageBean.setPrevPageNo(1);
		} else {
			pageBean.setPrevPageNo(maxPageNo - 1);
		}
		int limit = pageBean.getPageNum();
		int offset = (maxPageNo - 1) * limit;
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<RoleInfo> data = roleDao.getAllRoles(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String loadActions() throws Exception {
		com.googlecode.openauth.web.entity.RoleAction action = new com.googlecode.openauth.web.entity.RoleAction();
		action.setRoleId(roleId);
		roleActionList = this.roleDao.getRoleActions(action);
		actlist = actionDao.getAllAction();
		return SUCCESS;
	}

	public String loadMenus() throws Exception {
		RMenu pmenu = new RMenu();
		pmenu.setRoleId(roleId);
		rmenuList = this.roleDao.getRoleMenus(pmenu);
		MenuInfo menu = new MenuInfo();
		menulist = menuDao.getAllMenu(menu);
		return SUCCESS;
	}

	public String saveMenus() throws Exception {
		RMenu rmenu = new RMenu();
		rmenu.setRoleId(roleId);
		roleDao.delRoleMenuById((rmenu));
		for (int _menuId : menuIds) {
			RMenu rr = new RMenu();
			rr.setMenuId(_menuId);
			rr.setRoleId(roleId);
			roleDao.addRoleMenu(rr);
		}
		return SUCCESS;
	}

	public String saveResources() throws Exception {
		RRoleResource rrsource = new RRoleResource();
		rrsource.setRoleId(roleId);
		roleDao.delRoleResourceById(rrsource);
		for (int _resId : resIds) {
			RRoleResource rr = new RRoleResource();
			rr.setResourceId(_resId);
			rr.setRoleId(roleId);
			roleDao.addRoleResource(rr);
		}
		return SUCCESS;
	}

	public String saveActions() throws Exception {
		RRole ract = new RRole();
		ract.setRoleId(roleId);
		roleDao.delRoleActionById(ract);
		for (int _actionid : actionIds) {
			RRole rr = new RRole();
			rr.setActionId(_actionid);
			rr.setRoleId(roleId);
			roleDao.addRoleAction(rr);
		}
		return SUCCESS;
	}

	public String loadResources() throws Exception {
		RRoleResource rrr = new RRoleResource();
		rrr.setRoleId(roleId);
		roleResList = this.roleDao.getRoleResources(rrr);
		RoleInfo role=new RoleInfo();
		role.setRoleId(roleId);
		
		reslist = this.resourceDao.getAllResources(role,new RowBounds());
		return SUCCESS;
	}

	public String save() throws Exception {
		RoleInfo form = this.getForm();
		Timestamp createDate = new Timestamp(System.currentTimeMillis());
		form.setUpdateDate(createDate);	
		if (oper.equals("save")) {
			form.setCreateDate(createDate);
			UserInfo userInfo = getCurUser();
			form.setCreateUserId((int) userInfo.getUserId());
			roleDao.addRole(form);
			
			RRoleArea rra=new RRoleArea();
			rra.setRoleId(form.getRoleId());
			rra.setAreaId(areaId);
			relationDao.addRoleArea(rra);
			resetForm();
			return SUCCESS;
		} 
		else 
		{
			roleDao.updateRole(form);
			relationDao.delRoleAreaById(form.getRoleId());
			RRoleArea rra=new RRoleArea();
			rra.setRoleId(form.getRoleId());
			rra.setAreaId(areaId);
			relationDao.addRoleArea(rra);
			resetForm();
			this.roleId = 0;
			return SUCCESS;
		}
	}

	public String init() throws Exception {
		this.areaList=areaDao.getChildAreas(this.getCurRole());
		if (oper.equals("save")) { 
			return "new";
		} else {
			RoleInfo role = roleDao.getRoleById(roleId);
			setForm(role);
			return "update";
		}
	}

	public String delete() throws Exception {
		relationDao.delRoleActionById(roleIds[0]);
		relationDao.delRoleAreaById(roleIds[0]);
		relationDao.delRoleMenuById(roleIds[0]);
		relationDao.delRoleResourceById(roleIds[0]);
		roleDao.delRole(roleIds[0]);
		return SUCCESS;
	}

	public void setForm(RoleInfo role) {
		createDate = role.getCreateDate();
		createUserId = role.getRoleId();
		createUserId = role.getCreateUserId();
		roleDesc = role.getRoleDesc();
		roleId = role.getRoleId();
		roleName = role.getRoleName();
		updateDate = role.getUpdateDate();
		uuid = role.getUuid();
	}

	public RoleInfo getForm() {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		UserInfo usr = (UserInfo) session.get("user");

		RoleInfo role = new RoleInfo();
		role.setCreateDate(createDate);
		role.setCreateUserId((int) usr.getUserId());
		role.setRoleDesc(roleDesc);
		role.setRoleId(roleId);
		role.setRoleName(roleName);
		role.setUpdateDate(updateDate);
		role.setParent(usr.getRoleInfos().get(0).getRoleId());
		return role;
	}

	public void resetForm() {
		createDate = null;
		createUserId = 0;
		createUserId = 0;
		roleDesc = "";
		roleId = 0;
		roleName = "";
		updateDate = null;
		uuid = "";
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public void setList(List<RoleInfo> list) {
		this.list = list;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public List<ActionInfo> getActlist() {
		return actlist;
	}

	public void setActlist(List<ActionInfo> actlist) {
		this.actlist = actlist;
	}

	public int[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(int[] roleIds) {
		this.roleIds = roleIds;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public List<RoleInfo> getList() {
		return list;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getRoleDesc() {
		return roleDesc;
	}

	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}

	public int getCreateUserId() {
		return createUserId;
	}

	public void setCreateUserId(int createUserId) {
		this.createUserId = createUserId;
	}

	public Timestamp getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	public Timestamp getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Timestamp updateDate) {
		this.updateDate = updateDate;
	}

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public int[] getActionIds() {
		return actionIds;
	}

	public void setActionIds(int[] actionIds) {
		this.actionIds = actionIds;
	}

	public IActionDao getActionDao() {
		return actionDao;
	}

	public void setActionDao(IActionDao actionDao) {
		this.actionDao = actionDao;
	}

	public IResourceDao getResourceDao() {
		return resourceDao;
	}

	public void setResourceDao(IResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}

	public int[] getResIds() {
		return resIds;
	}

	public void setResIds(int[] resIds) {
		this.resIds = resIds;
	}

	public List<ResourceInfo> getReslist() {
		return reslist;
	}

	public void setReslist(List<ResourceInfo> reslist) {
		this.reslist = reslist;
	}

	public IRelationDao getRelationDao() {
		return relationDao;
	}

	public void setRelationDao(IRelationDao relationDao) {
		this.relationDao = relationDao;
	}

	public List<RRoleResource> getRoleResList() {
		return roleResList;
	}

	public void setRoleResList(List<RRoleResource> roleResList) {
		this.roleResList = roleResList;
	}

	public List<com.googlecode.openauth.web.entity.RoleAction> getRoleActionList() {
		return roleActionList;
	}

	public void setRoleActionList(
			List<com.googlecode.openauth.web.entity.RoleAction> roleActionList) {
		this.roleActionList = roleActionList;
	}

	public IMenuDao getMenuDao() {
		return menuDao;
	}

	public void setMenuDao(IMenuDao menuDao) {
		this.menuDao = menuDao;
	}

	public List<MenuInfo> getMenulist() {
		return menulist;
	}

	public void setMenulist(List<MenuInfo> menulist) {
		this.menulist = menulist;
	}

	public int[] getMenuIds() {
		return menuIds;
	}

	public void setMenuIds(int[] menuIds) {
		this.menuIds = menuIds;
	}

	public List<RMenu> getRmenuList() {
		return rmenuList;
	}

	public void setRmenuList(List<RMenu> rmenuList) {
		this.rmenuList = rmenuList;
	}

	// 鍒嗛〉鐢ㄥ埌鐨勫疄浣�
	public ActionPageBean<RoleInfo> getActionPageBean() {
		return actionPageBean;
	}

	public void setActionPageBean(ActionPageBean<RoleInfo> actionPageBean) {
		this.actionPageBean = actionPageBean;
	}

	public ActionPageBean<RoleInfo> getPageBean() {
		ActionPageBean<RoleInfo> bean = new ActionPageBean<RoleInfo>();
		bean.setCurrentPageNo(currentPageNo);
		bean.setPrevPageNo(prevPageNo);
		bean.setNextPageNo(nextPageNo);
		bean.setTotalCount(totalCount);
		bean.setPageNum(pageNum);
		return bean;
	}

	public void setPageBean(ActionPageBean<RoleInfo> bean) {
		currentPageNo = bean.getCurrentPageNo();
		prevPageNo = bean.getPrevPageNo();
		nextPageNo = bean.getNextPageNo();
		totalCount = bean.getTotalCount();
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getPrevPageNo() {
		return prevPageNo;
	}

	public void setPrevPageNo(int prevPageNo) {
		this.prevPageNo = prevPageNo;
	}

	public int getNextPageNo() {
		return nextPageNo;
	}

	public void setNextPageNo(int nextPageNo) {
		this.nextPageNo = nextPageNo;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public List<AreaInfo> getAreaList() {
		return areaList;
	}

	public void setAreaList(List<AreaInfo> areaList) {
		this.areaList = areaList;
	}
	public IAreaDao getAreaDao() {
		return areaDao;
	}
	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}
	public int getAreaId() {
		return areaId;
	}
	public void setAreaId(int areaId) {
		this.areaId = areaId;
	}

}
